/* icintrxr.p - Inventory Control Validations for Packs                       */
/* Copyright 1986 QAD Inc. All rights reserved.                               */
/* $Id:: icintrxr.p 65619 2016-01-29 16:42:02Z ika                         $: */
/*                                                                            */
 
{us/bbi/mfdeclre.i}
{us/gp/gprunpdf.i "gpglvpl" "p"}
{us/px/pxmaint.i}
{us/gp/gpuid.i}
{us/px/pxphdef.i paicxr}
{us/up/updaterestriction.i}
 
/* CONSIGNMENT VARIABLES. */
{us/so/socnvars.i}
 
 
/**-----------------------------------------------------------------------------
 * Verify GL Accounts.
 *
 * THIS SUBROUTINE DETERMINES THE VALIDITY OF THE ACCOUNT, SUB-ACCOUNT, COST
 * CENTER AND PROJECT USING THE PERSISTENT PROCEDURES
 *
 * @param acct        GL account number
 * @param sub         Sub GL account
 * @param cc          Cost center
 * @param proj        Project code for work order
 * @param acctfldname GL account number
 * @param glvalid     Whether GL account is valid
 *----------------------------------------------------------------------------*/
PROCEDURE verifyGlAccounts:
 
   define input  parameter acct        like trgl_dr_acct no-undo.
   define input  parameter sub         like trgl_dr_sub  no-undo.
   define input  parameter cc          like trgl_dr_cc   no-undo.
   define input  parameter proj        like wo_proj      no-undo.
   define input  parameter acctfldname like trgl_dr_acct no-undo.
   define output parameter glvalid     like mfc_logical initial true no-undo.
 
   /* INITIALIZE SETTINGS */
   {us/gp/gprunp.i "gpglvpl" "p" "initialize"}
 
   /* SET CONTROL FOR CHEKCING ACCOUNT GLTYPE/GLSYSTEMTYPE */
   {us/gp/gprunp.i "gpglvpl" "p" "set_acct_gltype_ver" "(input false)"}
 
   /* AP_ACCT/SUB/CC VALIDATION */
   {us/gp/gprunp.i "gpglvpl" "p" "validate_fullcode"
      "(input acct,
        input sub,
        input cc,
        input proj,
        input acctfldname,
        output glvalid)"}
 
END PROCEDURE. /* verifyGlAccounts */
 
/**-----------------------------------------------------------------------------
 * Check if Item, Site, Location & Account Combination is restricted.
 *
 * THIS SUBROUTINE DETERMINES THE VALIDITY OF THE ACCOUNT, SUB-ACCOUNT, COST
 * CENTER AND PROJECT USING THE PERSISTENT PROCEDURES
 *
 * @param pExecname   Execute program name
 * @param pPart       Item number
 * @param pSite       Site
 * @param pLoc        Location
 * @param pAcct       GL account number
 * @param pSub        Sub Gl account
 * @param pCC         Cost center
 * @param pProj       Project code for work order
 * @param pRestricted Whether it is restricted
 *----------------------------------------------------------------------------*/
PROCEDURE checkRestrictedCombination:
 
   define input  parameter pExecname   as   character     no-undo.
   define input  parameter pPart       like pt_part       no-undo.
   define input  parameter pSite       like sr_site       no-undo.
   define input  parameter pLoc        like sr_loc        no-undo.
   define input  parameter pAcct       like trgl_dr_acct  no-undo.
   define input  parameter pSub        like trgl_dr_sub   no-undo.
   define input  parameter pCC         like trgl_dr_cc    no-undo.
   define input  parameter pProj       like wo_proj       no-undo.
   define output parameter pRestricted like mfc_logical   no-undo.
 
   empty temp-table ttUpdateRestrictionSignature.
   create ttUpdateRestrictionSignature.
 
   assign
      ttUpdateRestrictionSignature.category              = {&UNP}
      ttUpdateRestrictionSignature.program               = pExecname
      ttUpdateRestrictionSignature.part                  = pPart
      ttUpdateRestrictionSignature.site                  = pSite
      ttUpdateRestrictionSignature.siteTo                = ?
      ttUpdateRestrictionSignature.location              = pLoc
      ttUpdateRestrictionSignature.locationTo            = ?
      ttUpdateRestrictionSignature.inventoryStatus       = ?
      ttUpdateRestrictionSignature.inventoryStatusTo     = ?
      ttUpdateRestrictionSignature.changeInventoryStatus = no
      ttUpdateRestrictionSignature.account               = pAcct
      ttUpdateRestrictionSignature.subAccount            = pSub
      ttUpdateRestrictionSignature.costcenter            = pCC
      ttUpdateRestrictionSignature.project               = pProj.
 
   pRestricted = isUpdateRestricted(input table ttUpdateRestrictionSignature,
                                    input yes).
 
END PROCEDURE. /* checkRestrictedCombination */
 
/**-----------------------------------------------------------------------------
 * Check Consignment Inventory.
 *
 * @param pTransType  Transaction type
 * @param pPart       Item number
 * @param pSite       Site
 * @param pLoc        Location
 * @param pLotser     Lot/Serial
 * @param pRef        Reference
 * @param pQty        Quantity
 * @param pOrderNbr   Order number
 * @param pOrderLine  Order line
 * @param pErrorFound Whether the error is found
 *----------------------------------------------------------------------------*/
PROCEDURE validateConsignment:
 
   define input  parameter pTransType    as   character  no-undo.
   define input  parameter pPart         like pt_part    no-undo.
   define input  parameter pSite         like sr_site    no-undo.
   define input  parameter pLoc          like sr_loc     no-undo.
   define input  parameter pLotser       like sr_lotser  no-undo.
   define input  parameter pRef          like sr_ref     no-undo.
   define input  parameter pQty          as   decimal    no-undo.
   define input  parameter pOrderNbr     like tr_nbr     no-undo.
   define input  parameter pOrderLine    like tr_line    no-undo.
   define output parameter pErrorFound   as   logical    no-undo.
 
   define variable consignedLine  like mfc_logical  no-undo.
 
   if ((pTransType = "ISS-UNP"  and pQty < 0) or
       (pTransType = "RCT-UNP"  and pQty > 0)) and
      pOrderNbr <> "" and pQty <> 0
   then do:
 
      /*Check sales order to see if it is consigned and      */
      /* that the transfer item matches the sales order item*/
      run checkConsignedOrder
         (input pOrderNbr,
          input pOrderLine,
          input pPart,
          output consignedLine,
          output pErrorFound).
 
      if pErrorFound then do:
         /* MATCHING ORDER LINE ITEM FOR THIS PART NOT FOUND*/
         {us/bbi/pxmsg.i &MSGNUM=8285 &ERRORLEVEL=3}
         return {&APP-ERROR-RESULT}.
      end.
 
   end.
 
   if ((pTransType = "ISS-UNP"  and pQty > 0) or
       (pTransType = "RCT-UNP"  and pQty < 0)) and
      pQty <> 0
   then do:
 
      run checkConsignmentInventory
         (input pOrderNbr,
          input pOrderLine,
          input pSite,
          input pPart,
          input pLoc,
          input pLotser,
          input pRef,
          input pQty,
          output pErrorFound).
 
      if pErrorFound
         then do:
         /* UNABLE TO ISSUE/RECEIVE CONSIGNED INVENTORY*/
         {us/bbi/pxmsg.i &MSGNUM=4937 &ERRORLEVEL=3}
         return {&APP-ERROR-RESULT}.
      end.
 
   end.
 
 
END PROCEDURE. /* validateConsignment */
 
/**-----------------------------------------------------------------------------
 * Check Consignment Order.
 *
 * @param ip_ordernbr  Order number
 * @param ip_orderline Order line
 * @param ip_part      Item number
 * @param op_consigned Whether order is consigned
 * @param op_error     Whehter there is error
 *----------------------------------------------------------------------------*/
PROCEDURE checkConsignedOrder:
 
   define input parameter ip_ordernbr    as character no-undo.
   define input parameter ip_orderline   as integer   no-undo.
   define input parameter ip_part        as character no-undo.
   define output parameter op_consigned  as logical   no-undo.
   define output parameter op_error      as logical   no-undo.
 
   op_consigned = no.
 
   {us/gp/gprunmo.i
      &program = "socnsod1.p"
      &module = "ACN"
      &param = """(input ip_ordernbr,
                   input ip_orderline,
                   output op_consigned,
                   output consign_loc,
                   output intrans_loc,
                   output max_aging_days,
                   output auto_replenish)"""}
 
   if op_consigned then do:
      if can-find(first sod_det where
                        sod_domain = global_domain and
                        sod_nbr = ip_ordernbr and
                        sod_line = ip_orderline and
                        sod_part = ip_part and
                        sod_compl_stat = "")
      then
         op_error = no.
      else
         op_error = yes.
   end. /* IF op_consigned */
 
END PROCEDURE. /* checkConsignedOrder */
 
/**-----------------------------------------------------------------------------
 * Check Consignment Inventory.
 *
 * @param ip_ordernbr  Order number
 * @param ip_orderline Order line
 * @param ip_site      Site
 * @param ip_part      Item number
 * @param ip_location  Location
 * @param ip_lotser    Lot/Serial
 * @param ip_ref       Reference
 * @param ip_tran_qty  Transfered quantity
 * @param op_error     Whether there is error
 *----------------------------------------------------------------------------*/
PROCEDURE checkConsignmentInventory:
 
   define input  parameter ip_ordernbr    as character no-undo.
   define input  parameter ip_orderline   as character no-undo.
   define input  parameter ip_site        like ld_site no-undo.
   define input  parameter ip_part        like ld_part no-undo.
   define input  parameter ip_location    like ld_loc  no-undo.
   define input  parameter ip_lotser      like ld_lot  no-undo.
   define input  parameter ip_ref         like ld_ref  no-undo.
   define input  parameter ip_tran_qty    as decimal   no-undo.
   define output parameter op_error      as logical   no-undo.
 
   define variable consigned_line        as logical   no-undo.
   define variable unconsigned_qty       as decimal   no-undo.
   define variable consigned_qty_oh      as decimal   no-undo.
   define variable location_qty          as decimal   no-undo.
   define variable procid                as character no-undo.
 
   /*IF A SALES ORDER WAS ENTERED, CHECK WHETHER IT IS FOR A   */
   /*CONSIGNED ITEM.                                           */
 
   if ip_ordernbr <> "" then do:
      run checkConsignedOrder
         (input ip_ordernbr,
          input ip_orderline,
          input ip_part,
          output consigned_line,
          output op_error).
      if consigned_line then
         op_error = yes.
      else
         op_error = no.
   end.
 
   if not op_error then do:
      /*IF CONSIGNED, FIND OUT HOW MUCH NON-CONSIGNED INVENTORY   */
      /*IS AT THE LOCATION. IF THERE IS NOT ENOUGH TO COVER THE   */
      /*QTY BEING ISSUED, THEN ERROR.                             */
 
      /*RETRIEVE THE TOTAL QTY ON-HAND FOR THE LOCATION */
      for first ld_det
           where ld_domain = global_domain and
                 ld_part = ip_part     and
                 ld_site = ip_site     and
                 ld_loc  = ip_location and
                 ld_lot  = ip_lotser   and
                 ld_ref  = ip_ref
      no-lock:
         assign
             location_qty  = ld_qty_oh
             consigned_qty_oh = ld_cust_consign_qty.
 
      end. /*for first ld_det*/
 
      unconsigned_qty = location_qty - consigned_qty_oh.
 
      if (consigned_qty_oh <> 0) and
         ((ip_tran_qty > 0 and unconsigned_qty < ip_tran_qty) or
          (ip_tran_qty < 0 and unconsigned_qty < (ip_tran_qty * -1)))
      then
        op_error = yes.
 
   end.
 
END PROCEDURE. /* checkConsignmentInventory */
 
PROCEDURE isOverIssueAllowed:
 
   define input parameter ipItem as character no-undo.
   define input parameter ipSite as character no-undo.
   define input parameter ipLoc as character no-undo.
   define input parameter ipLotser as character no-undo.
   define input parameter ipRef as character no-undo.
   define output parameter opOverIssue as logical no-undo.
 
   define buffer b_ld_det for ld_det.
   define buffer b_loc_mstr for loc_mstr.
   define buffer b_si_mstr for si_mstr.
   define buffer b_is_mstr for is_mstr.
   define variable mIsStatus like is_status no-undo.
 
   assign
      opOverIssue = false
      mIsStatus = "".
 
   /* check is status */
   check_is:
   do on error undo, retry:
 
      for first b_ld_det no-lock
         where b_ld_det.ld_domain = global_domain
           and b_ld_det.ld_site = ipSite
           and b_ld_det.ld_loc = ipLoc
           and b_ld_det.ld_lot = ipLotser
           and b_ld_det.ld_part = ipItem
           and b_ld_det.ld_ref = ipRef:
      end.
 
      if available b_ld_det then do:
         assign
            mIsStatus = b_ld_det.ld_status.
         leave check_is.   
      end.   
 
      for first b_loc_mstr no-lock
         where b_loc_mstr.loc_domain = global_domain
           and b_loc_mstr.loc_site = ipSite
           and b_loc_mstr.loc_loc = ipLoc:
      end.
 
      if available b_loc_mstr then do:
         assign
            mIsStatus = b_loc_mstr.loc_status.
         leave check_is.   
      end.
 
      for first b_si_mstr no-lock
         where b_si_mstr.si_domain = global_domain
           and b_si_mstr.si_site = ipSite:
      end.
 
      if available b_si_mstr then do:
         assign
            mIsStatus = b_si_mstr.si_status.
         leave check_is.   
      end.
 
   end. /* check_is */
 
   for first b_is_mstr no-lock
      where b_is_mstr.is_domain = global_domain
        and b_is_mstr.is_status = mIsStatus:
   end.
 
   if available b_is_mstr then do:
 
      assign
         opOverIssue = b_is_mstr.is_overissue.
 
   end.   
 
END PROCEDURE.
 
/**-----------------------------------------------------------------------------
 * Validate Non-Serialized Loose item Qty 
 *
 * @param woLot  
 * @param sessionId            
 * @param allowToIssue        
 *----------------------------------------------------------------------------*/
PROCEDURE validateNonSerLooseQtyForIssue:
   define input  parameter part         like pt_part            no-undo.
   define input  parameter sessionId    like sr_userid          no-undo.
   define output parameter allowToIssue as logical initial no no-undo.
   define output parameter failedPart   like wod_part no-undo.
   define output parameter looseQty     like sr_qty no-undo.
 
   define buffer sr_wkfl   for   sr_wkfl.
 
   for each sr_wkfl no-lock
      where sr_domain = global_domain
        and sr_userid = SessionUniqueID
        and sr_lineid = "":
 
      {us/px/pxrun.i &PROC = 'allowIssueWithoutSerialization'
                        &PROGRAM='paicxr.p'
                        &HANDLE = ph_paicxr
                        &PARAM = "(input part,
                                   input sr_site,
                                   input sr_loc,
                                   input sr_lotser,
                                   input sr_ref,
                                   input sr_qty,
                                   output allowToIssue,
                                   output looseQty)"
                        &NOAPPERROR = true
                        &CATCHERROR = true}
 
      if not allowToIssue then do:  
         failedPart = part.
         return {&APP-ERROR-RESULT}.
      end.
   end.
 
   allowToIssue   = yes.
   return {&SUCCESS-RESULT}.
 
END PROCEDURE. 

PROCEDURE checkAvailNonConsignedInv:
/*------------------------------------------------------------------------------
Purpose: During MO shipment, consigned inventory should not be allowed to
         transfer across different sites, if the Transfer Ownership flag is No.
         Check sufficient inventory is present in site/loc/lotserial/ref 
         during MO shipment for an item across all lines for the order      

Parameters: p_recid            - recid of sod_det, current SO Line 
            p_SessionUniqueID  - unique session ID generated for the sr_wkfl
            p_site             - site from where inventory is being issued
            p_location         - location from where inventory is being issued
            p_lotserial        - lotserial for the stock
            p_lotref           - inventory reference
            p_site-to          - site to which inventory is being moved
            p_loc-to           - location to which inventory is being moved
            p_lotserial_qty    - quantity being moved 
            p_err_flag         - output flag set to yes if insufficient stock

Exceptions:  NONE
Notes:
History:
------------------------------------------------------------------------------*/

   define input  parameter  p_recid           as recid     no-undo.
   define input  parameter  p_SessionUniqueID as character no-undo.
   define input  parameter  p_site            as character no-undo.
   define input  parameter  p_location        as character no-undo.
   define input  parameter  p_lotserial       as character no-undo.
   define input  parameter  p_lotref          as character no-undo.
   define input  parameter  p_site-to         as character no-undo.
   define input  parameter  p_loc-to          as character no-undo.
   define input  parameter  p_lotserial_qty   as decimal   no-undo.
   define output parameter  p_err_flag        as logical   no-undo.

   define variable          l_total           as decimal   no-undo.

   define buffer soddet  for sod_det.
   define buffer sod_det for sod_det.
   define buffer srwkfl  for sr_wkfl.   

   for first sod_det 
      where recid(sod_det) = p_recid
   no-lock:
      l_total = p_lotserial_qty * sod_det.sod_um_conv.

      for first ld_det
         where ld_det.ld_domain = global_domain
         and   ld_det.ld_part   = sod_det.sod_part
         and   ld_det.ld_site   = p_site
         and   ld_det.ld_loc    = p_location
         and   ld_det.ld_lot    = p_lotserial
         and   ld_det.ld_ref    = p_lotref
      no-lock:

         /* TOTAL THE ITEM QTY ACROSS SO LINES */
         for each soddet
            where soddet.sod_domain     = global_domain
            and   soddet.sod_nbr        = sod_det.sod_nbr
            and   soddet.sod_part       = sod_det.sod_part
            and   soddet.sod_line       <> sod_det.sod_line
            and   soddet.sod_compl_stat =  ""
         no-lock:
            for first srwkfl               
               where srwkfl.sr_domain = global_domain
               and srwkfl.sr_userid   = p_SessionUniqueID
               and srwkfl.sr_lineid   = string(soddet.sod_line )
               and srwkfl.sr_site     = p_site
               and srwkfl.sr_loc      = p_location
               and srwkfl.sr_lotser   = p_lotserial
               and srwkfl.sr_ref      = p_lotref
            no-lock:
               l_total = l_total + (srwkfl.sr_qty * sod_um_conv).
            end. /* FOR EACH sr_wkfl */
         end. /* FOR EACH soddet */

         /* CHECK FOR NON-CONSIGNED INVENTORY PRESENT */
         {us/bbi/gprun.i ""iccnchk.p""
                          "( input  p_site,
                             input  p_site-to,
                             input  p_loc-to,
                             input  p_lotserial,
                             input  p_lotref,
                             input  l_total,
                             input  ld_det.ld_qty_oh,
                             input  p_location,
                             output p_err_flag)"}
      end. /* FOR FIRST ld_det */     
   end. /* FOR FIRST sod_det */   
END PROCEDURE. /* checkAvailNonConsignedInv */

